package com.haier.controller.manager;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.haier.constant.HpConstant;
import com.haier.dto.HpGoApplyDto;
import com.haier.utils.Servlets;
import com.haier.utils.XlsxExcelCreation;
import com.haier.vo.HpAreaGoTrackVo;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.haier.dto.HpAreaGoTrackDto;
import com.haier.service.IHpAreaGoTrackService;
import com.haier.utils.EmptyUtils;
import com.haier.utils.GetUtils;
import com.haier.vo.ResponseVo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 轨迹查询
 */
@RestController
@RequestMapping(value = "/hipassjava/manager/hpAreaGoTrack")
public class ManagerHpAreaGoTrackContoller {
	private static final Logger logger = LoggerFactory.getLogger(ManagerHpAreaGoTrackContoller.class);
	@Autowired
	private IHpAreaGoTrackService iHpAreaGoTrackService;

	/**
	 * 查询分页
	 */
	@RequestMapping(value = "/page", method = RequestMethod.POST)
	public Object page(@RequestBody HpAreaGoTrackDto dto) {
		try {
			return ResponseVo.success(iHpAreaGoTrackService.page(dto));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return ResponseVo.error();
		}
	}

	/**
	 * 查询历史数据分页
	 */
	@RequestMapping(value = "/pageHistory", method = RequestMethod.POST)
	public Object pageHistory(@RequestBody HpAreaGoTrackDto dto) {
		try {
			return ResponseVo.success(iHpAreaGoTrackService.pageHistory(dto));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return ResponseVo.error();
		}
	}

	/**
	 * 导出当天实时数据
	 */
	@RequestMapping(value = "/reportTotayHoursCount", method = RequestMethod.POST)
	public Object reportTotayHoursCount(@RequestBody HpAreaGoTrackDto dto) {
		try {
			String dateStr = GetUtils.getDateString(new Date(), "yyyy-MM-dd");
			String startTime = dateStr + " 00";
			String endTime = dateStr + " 23";
			dto.setStartTime(startTime);
			dto.setEndTime(endTime);
			dto.setIsSuccess(true);
			return ResponseVo.success(iHpAreaGoTrackService.reportTotayHoursCount(dto));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return ResponseVo.error();
		}
	}

	/**
	 * 导出上周的数量
	 */
	@RequestMapping(value = "/reportWeekDayCount", method = RequestMethod.POST)
	public Object reportWeekDayCount(@RequestBody HpAreaGoTrackDto dto) {
		try {
			if (EmptyUtils.isEmpty(dto.getAreaIds())) {
				return ResponseVo.checkError("区域ids不能为空");
			}
			return ResponseVo.success(iHpAreaGoTrackService.reportWeekDayCount(dto));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return ResponseVo.error();
		}
	}

	/**
	 * 导出小时数量
	 */
	@RequestMapping(value = "/reportHoursCount", method = RequestMethod.POST)
	public Object reportHoursCount(@RequestBody HpAreaGoTrackDto dto) {
		try {
			if (EmptyUtils.isEmpty(dto.getAreaIds())) {
				return ResponseVo.checkError("区域ids不能为空");
			}
			if (EmptyUtils.isEmpty(dto.getStartTime())) {
				return ResponseVo.checkError("开始时间不能为空");
			}
			if (EmptyUtils.isEmpty(dto.getEndTime())) {
				return ResponseVo.checkError("结束时间不能为空");
			}
			return ResponseVo.success(iHpAreaGoTrackService.reportHoursCount(dto));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return ResponseVo.error();
		}
	}

	/**
	 * 导出天数量
	 */
	@RequestMapping(value = "/reportDayCount", method = RequestMethod.POST)
	public Object reportDayCount(@RequestBody HpAreaGoTrackDto dto) {
		try {
			if (EmptyUtils.isEmpty(dto.getAreaIds())) {
				return ResponseVo.checkError("区域ids不能为空");
			}
			if (EmptyUtils.isEmpty(dto.getStartTime())) {
				return ResponseVo.checkError("开始时间不能为空");
			}
			if (EmptyUtils.isEmpty(dto.getEndTime())) {
				return ResponseVo.checkError("结束时间不能为空");
			}
			return ResponseVo.success(iHpAreaGoTrackService.reportDayCount(dto));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return ResponseVo.error();
		}
	}

	/**
	 * 导出月数量
	 */
	@RequestMapping(value = "/reportMonthCount", method = RequestMethod.POST)
	public Object reportMonthCount(@RequestBody HpAreaGoTrackDto dto) {
		try {
			if (EmptyUtils.isEmpty(dto.getAreaIds())) {
				return ResponseVo.checkError("区域ids不能为空");
			}
			if (EmptyUtils.isEmpty(dto.getStartTime())) {
				return ResponseVo.checkError("开始时间不能为空");
			}
			if (EmptyUtils.isEmpty(dto.getEndTime())) {
				return ResponseVo.checkError("结束时间不能为空");
			}
			return ResponseVo.success(iHpAreaGoTrackService.reportMonthCount(dto));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return ResponseVo.error();
		}
	}

	/**
	 * 导出 未用
	 * @throws IOException
	 */
	//@RequestMapping(value = "/reportTrack", method = RequestMethod.GET)
	public void reportApply(HttpServletResponse response, HpAreaGoTrackDto dto) throws IOException {
		String filename = GetUtils.getDateString(new Date(), "yyyyMMddHHmmss")
				+ URLEncoder.encode("轨迹报表.xlsx", "UTF-8");
		response.setContentType("application/octet-stream; charset=UTF-8");
		response.setHeader("Content-Disposition", "attachment; filename=" + filename);
		SXSSFWorkbook book = iHpAreaGoTrackService.reportTrack(dto);
		book.write(response.getOutputStream());
	}


	/**
	 * 导出
	 * @param request
	 * @param response
	 * @param dto
	 */
	@RequestMapping(value = "/reportTrack", method = RequestMethod.GET)
	public void exportList(HttpServletRequest request,
						   HttpServletResponse response,
						   HpAreaGoTrackDto dto) {
		response.setContentType(Servlets.EXCEL_TYPE);
		Servlets.setFileDownloadHeader(request, response, "轨迹报表.xlsx");
		try {
			OutputStream outputStream = response.getOutputStream();
			//导出Excel
			int[] columnWidth = {
					3000, 3000, 3000, 6000, 6000, 3000, 6000, 5000, 6000,5000
			};
			String[][][] headNames = {{
					{"工号"}, {"姓名"}, {"园区"}, {"区域"},{"时间"}
			}};
			String[][] valueName = {
					{"jobNumber"}, {"name"}, {"garden"}, {"area"}, {"timeStr"}
			};
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			List<HpAreaGoTrackVo> list = iHpAreaGoTrackService.getListReport(dto);
			for (HpAreaGoTrackVo avo:list){
				String dateString = formatter.format(avo.getTime());
				avo.setTimeStr(dateString);
			}
			XlsxExcelCreation<HpAreaGoTrackVo> creation =
					new XlsxExcelCreation<>(list, outputStream, columnWidth, headNames, valueName);
			creation.createXlsx();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
